package netWork.练习1_聊天室.多线程实现;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Package netWork.练习1_聊天室.多线程实现
 * @Author shimmer
 * @Date 2020/8/2 14:00
 * @TODO 发送线程
 */
public class SendThread implements Runnable{
    //获取socket对象
    Socket socket=null;
    DataOutputStream os=null;
    Scanner scanner=null;
    public SendThread(Socket socket) {
        this.socket=socket;
    }
    
    @Override
    public void run() {
        try {
            os = new DataOutputStream(socket.getOutputStream());
            scanner = new Scanner(System.in);
            while (true){
                os.writeUTF(scanner.nextLine());
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (scanner!=null){
                scanner.close();
            }
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
